//
//  NMyMsgViewController.m
//  News
//
//  Created by zhangtao on 2018/10/16.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NMyMsgViewController.h"
#import "NWalletDetailHeaderView.h"
#import "NMyMsgNotyCell.h"
#import "NMyMsgAdCell.h"
#import "NoticeListModel.h"

@interface NMyMsgViewController ()<UITableViewDelegate,UITableViewDataSource>
#define notyIdentifier @"notyIdentifier"
#define adIdentifier @"adIdentifier"
@property(nonatomic,strong)UITableView *table;
@property(nonatomic,assign)NSInteger type;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation NMyMsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的消息";
    self.view.backgroundColor = [UIColor whiteColor];
    NWalletDetailHeaderView *top = [[NWalletDetailHeaderView alloc]initWithFrame:CGRectMake(0, navHeight, ACEWidth, 40) items:@[@"通知",@"公告"]];
    [self.view addSubview:top];
    self.type = 4;
    WEAKSELF;
    top.WalletDetailViewCallBack = ^(NSInteger tag) {
        if (tag == 0) {
            weakSelf.type = 4;
        } else {
            weakSelf.type = tag;
        }
        [self refreshAction];
    };
    
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight+40, ACEWidth, ACEHeight-navHeight-40)  style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table registerNib:[UINib nibWithNibName:@"NMyMsgNotyCell" bundle:nil] forCellReuseIdentifier:notyIdentifier];
    [_table registerNib:[UINib nibWithNibName:@"NMyMsgAdCell" bundle:nil] forCellReuseIdentifier:adIdentifier];
    [self addRefreshFunction];
    [self addLoadMoreFunction];
}

#pragma mark AddTableViewPlugin
- (void)addRefreshFunction
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader
                                     headerWithRefreshingTarget:self
                                     refreshingAction:@selector(refreshAction)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    self.table.mj_header = header;
}

- (void)addLoadMoreFunction
{
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter
                                        footerWithRefreshingTarget:self
                                        refreshingAction:@selector(loadMoreAction)];
    // 设置footer
    self.table.mj_footer = footer;
    self.table.mj_footer.automaticallyHidden = YES;
}

#pragma mark RefreshAction
- (void)refreshAction
{
    self.pageIndex = 1;
    [self getData:1];
}

- (void)loadMoreAction
{
    self.pageIndex ++;
    [self getData:self.pageIndex];
}

- (void)getData:(NSInteger)pageIndex
{
    [[NHTTPManage shareInstance]afGet:HTTP_GET_GetList andParam:@{@"token":TOKEN,@"cuid":CUID,@"cp":@(pageIndex),@"ntype":@(self.type)} success:^(id  _Nonnull responseObject) {
        //结束头部刷新
        [self.table.mj_header endRefreshing];
        //结束尾部刷新
        [self.table.mj_footer endRefreshing];
        if (self.dataArray.count > 0 && self.pageIndex == 1) {
            [self.dataArray removeAllObjects];
        }
        [responseObject[@"Data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NoticeListModel *model = [NoticeListModel modelObjectWithDictionary:obj];
            [self.dataArray addObject:model];
        }];
        if ([responseObject[@"Data"] count] == 0) {
            [self.table.mj_footer endRefreshingWithNoMoreData];
        }
        [self.table reloadData];
    } failture:^(id  _Nonnull error) {
        //结束头部刷新
        [self.table.mj_header endRefreshing];
        //结束尾部刷新
        [self.table.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_type == 2) {
        NMyMsgNotyCell *cell = [tableView dequeueReusableCellWithIdentifier:notyIdentifier];
        return cell;
    }else{
        NMyMsgAdCell *cell = [tableView dequeueReusableCellWithIdentifier:adIdentifier];
        [cell bindData:self.dataArray[indexPath.row]];
        return cell;
    }
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
